package com.hospital.register.order.controller.website;

import com.hospital.register.common.result.Result;
import com.hospital.register.order.service.WxpayService;
import com.hospital.register.vo.order.OrderPayQrDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Api(tags = "微信支付", description = "面向用户")
@RestController
@RequestMapping("/api/order/weixin")
@Validated
public class WxpaySiteController {

    /**
     * 处理微信支付相关业务
     */
    @Autowired
    private WxpayService wxpayService;

    @ApiOperation("生成微信支付二维码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping("/createNative/{orderId}")
    public Result<OrderPayQrDto> createNative(
            @PathVariable @NotNull(message = "订单 id 不可为空") @Min(value = 0, message = "订单 id 非法") Long orderId) {
        return Result.ok(this.wxpayService.willPayment(orderId));
    }

    @ApiOperation("查询支付状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping("/queryPayStatus/{orderId}")
    public Result queryPayStatus(
            @PathVariable @Min(value = 0L, message = "订单 id 非法") Long orderId) {
        return this.wxpayService.checkPayStatus(orderId);
    }

}
